package com.sansi.pencilbook.service.impl;

import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.entity.Book;
import com.sansi.pencilbook.entity.Category;
import com.sansi.pencilbook.entity.StoreBook;
import com.sansi.pencilbook.entity.StoreBookDTO;
import com.sansi.pencilbook.mapper.StoreBookMapper;
import com.sansi.pencilbook.service.BookServer;
import com.sansi.pencilbook.service.StoreBookServer;
import com.sansi.pencilbook.util.ExcelUtil;
import com.sansi.pencilbook.util.ISBNUtil;
import jxl.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.Stroke;
import java.io.File;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 功能说明: 书店的书籍服务
 *
 * @author lsy13
 */
@Service
public class StoreBookServerImpl implements StoreBookServer {

    @Autowired
    private StoreBookMapper storeBookMapper;

    @Autowired
    private BookServer bookServer;

    @Override
    public Result getAllStoreBooks() {
        try {
            List<StoreBookDTO> list = storeBookMapper.findAll();
            return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
                Result.ofFailed("查询为空");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result getStoreBooksByStore(int storeId) {
        try {
            List<StoreBookDTO> list = storeBookMapper.findByStore(storeId);
            return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
                Result.ofFailed("查询为空");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result getStoreBookByIsbn(String isbn) {
        try {
            List<StoreBookDTO> list = storeBookMapper.findByBookIsbn(isbn);
            return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
                Result.ofFailed("查询为空");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result getStoreBookByStoreAndIsbn(int storeId, String isbn) {
        try {
            StoreBookDTO storeBook = storeBookMapper.findStoreNameByStoreAndIsbn(storeId, isbn);
            return storeBook!=null ? Result.ofSuccess("查询成功", 1, storeBook) :
                Result.ofFailed("查询为空");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result addStoreBook(StoreBook storeBook) {
        try {
            return storeBookMapper.saveStoreBook(storeBook)==1 ? Result.ofSuccess("添加成功", 1) :
                Result.ofFailed("重复添加");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("添加失败");
        }
    }

    @Override
    public Result addStoreBookByExcel(File xlsFile,int storeId) {
        try {
            Sheet sheet = ExcelUtil.getSheet(xlsFile, 0);
            if (sheet.getColumns() != 5) {
                return Result.ofFailed("表格上传错误");
            }
            List<StoreBook> books = new ArrayList<>();
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            Calendar calendar = Calendar.getInstance();
            for (int row=1;row<sheet.getRows();row++) {
                StoreBook storeBook = new StoreBook();
                Book book = new Book();
                storeBook.setStoreId(storeId);
                storeBook.setTime(format.format(calendar.getTime()));
                for (int col=0;col<sheet.getColumns();col++) {
                    switch (sheet.getCell(col,0).getContents()) {
                        case "ISBN" :
                            String isbn = sheet.getCell(col,row).getContents();
                            if (ISBNUtil.checkISBN(isbn) && (book=(Book) (bookServer.getBookByIsbn(isbn).getData()))!=null) {
                                storeBook.setBookIsbn(sheet.getCell(col,row).getContents());
                            }else {
                                return Result.ofFailed("第" + (row+1) + "行图书的ISBN不正确或者不存在");
                            }
                            break;
                        case "库存" :
                            storeBook.setNumber(Integer.parseInt(sheet.getCell(col,row).getContents()));
                            break;
                        case "价格" :
                            storeBook.setDiscountPrice(Float.parseFloat(sheet.getCell(col,row).getContents()));
                            break;
                        case "推荐语" :
                            storeBook.setIntroduction(sheet.getCell(col,row).getContents());
                            break;
                        case "备注" :
                            storeBook.setRemark(sheet.getCell(col,row).getContents());
                            break;
                    }
                }
                storeBook.setDiscount(storeBook.getDiscountPrice()/book.getBookPrice());
                storeBook.setImg(book.getBookImg());
                storeBook.setPrice(book.getBookPrice());
                books.add(storeBook);
            }
            int num = storeBookMapper.saveStoreBooks(books);
            return num>0 ? Result.ofSuccess("批量插入成功", num) :
                Result.ofFailed("批量插入失败");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return Result.ofFailed(e.getMessage());
        }
    }

    @Override
    public Result modifyStoreBook(StoreBook storeBook) {
        try {
            return storeBookMapper.updateStoreBook(storeBook)==1 ? Result.ofSuccess("修改成功", 1) :
                Result.ofFailed("重复修改");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("修改失败");
        }
    }

    @Override
    public Result modifyStoreBooks(List<StoreBook> storeBooks) {
        try {
            int num = storeBookMapper.updateStoreBooks(storeBooks);
            return num>0 ? Result.ofSuccess("批量修改书籍成功", num) :
                Result.ofFailed("批量修改书籍失败");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("修改书籍失败");
        }
    }

    @Override
    public Result modifyStoreBooksByExcel(File xlsFile,int storeId) {
        try {
            Sheet sheet = ExcelUtil.getSheet(xlsFile, 0);
            if (sheet.getColumns() != 5) {
                return Result.ofFailed("表格上传错误");
            }
            List<StoreBook> books = new ArrayList<>();
            for (int row=1;row<sheet.getRows();row++) {
                StoreBook storeBook = null;
                for (int col=0;col<sheet.getColumns();col++) {
                    switch (sheet.getCell(col,0).getContents()) {
                        case "ISBN" :
                            String isbn = sheet.getCell(col,row).getContents();
                            if (ISBNUtil.checkISBN(isbn) && (storeBook=storeBookMapper.findByStoreAndIsbn(storeId, isbn))!=null) {
                                storeBook.setBookIsbn(sheet.getCell(col,row).getContents());
                            }else {
                                return Result.ofFailed("图书的ISBN不正确或者不存在");
                            }
                            break;
                        case "库存" :
                            assert storeBook != null;
                            String number = sheet.getCell(col,row).getContents();
                            if (!"".equals(number))
                                storeBook.setNumber(Integer.parseInt(number));
                            break;
                        case "价格" :
                            assert storeBook != null;
                            String price = sheet.getCell(col,row).getContents();
                            if (!"".equals(price) && !"0".equals(price))
                                storeBook.setDiscountPrice(Float.parseFloat(price));
                            break;
                        case "推荐语" :
                            assert storeBook != null;
                            String introduction = sheet.getCell(col,row).getContents();
                            if (!"".equals(introduction))
                                storeBook.setIntroduction(sheet.getCell(col,row).getContents());
                            break;
                        case "备注" :
                            assert storeBook != null;
                            String remark = sheet.getCell(col,row).getContents();
                            if (!"".equals(remark))
                            storeBook.setRemark(remark);
                            break;
                    }
                }
                assert storeBook != null;
                storeBook.setDiscount(storeBook.getDiscountPrice()/storeBook.getPrice());
                books.add(storeBook);
            }
            int num = storeBookMapper.updateStoreBooks(books);
            return num>0 ? Result.ofSuccess("批量修改成功", num) :
                Result.ofFailed("没有需要修改的图书");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return Result.ofFailed(e.getMessage());
        }
    }

    @Override
    public Result modifyStoreBooksPriceByExcel(File xlsFile,int storeId) {
        try {
            Sheet sheet = ExcelUtil.getSheet(xlsFile, 0);
            if (sheet.getColumns() != 3) {
                return Result.ofFailed("表格上传错误");
            }
            List<StoreBook> books = new ArrayList<>();
            for (int row=1;row<sheet.getRows();row++) {
                StoreBook storeBook = null;
                for (int col=0;col<sheet.getColumns();col++) {
                    switch (sheet.getCell(col,0).getContents()) {
                        case "ISBN" :
                            String isbn = sheet.getCell(col,row).getContents();
                            if (ISBNUtil.checkISBN(isbn) && (storeBook=storeBookMapper.findByStoreAndIsbn(storeId, isbn))!=null) {
                                storeBook.setBookIsbn(sheet.getCell(col,row).getContents());
                            }else {
                                return Result.ofFailed("图书的ISBN不正确或者不存在");
                            }
                            break;
                        case "库存" :
                            assert storeBook != null;
                            String number = sheet.getCell(col,row).getContents();
                            if (!"".equals(number))
                                storeBook.setNumber(Integer.parseInt(number));
                            break;
                        case "价格" :
                            assert storeBook != null;
                            String price = sheet.getCell(col,row).getContents();
                            if (!"".equals(price) && !"0".equals(price))
                                storeBook.setDiscountPrice(Float.parseFloat(price));
                            break;
                    }
                }
                assert storeBook != null;
                storeBook.setDiscount(storeBook.getDiscountPrice()/storeBook.getPrice());
                books.add(storeBook);
            }
            int num = storeBookMapper.updateStoreBooks(books);
            return num>0 ? Result.ofSuccess("批量修改成功", num) :
                Result.ofFailed("没有需要修改的图书");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return Result.ofFailed(e.getMessage());
        }
    }

    @Override
    public Result deleteStoreBook(StoreBook storeBook) {
        try {
            return storeBookMapper.deleteStoreBook(storeBook)==1 ? Result.ofSuccess("删除成功", 1) :
                Result.ofFailed("重复删除");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("删除失败");
        }
    }

    @Override
    public Result modifyCountAndPrice(File xlsFile, int storeId) {
        try {
            String[] isbns = ExcelUtil.getColData(xlsFile, 0, 0);
            String[] counts = ExcelUtil.getColData(xlsFile, 0, 1);
            String[] prices = ExcelUtil.getColData(xlsFile, 0, 2);
            List<StoreBook> list = new ArrayList<>();
            for (int i=0;i<isbns.length;i++) {
                StoreBook storeBook = storeBookMapper.findByStoreAndIsbn(storeId, isbns[i]);
                storeBook.setNumber((counts[i]!=null || counts[i]!="") ? Integer.parseInt(counts[i]) : storeBook.getNumber());
                storeBook.setPrice((prices[i]!= null || prices[i]!="") ? Float.parseFloat(prices[i]) : storeBook.getPrice());
                list.add(storeBook);
            }
            int num = storeBookMapper.updateStoreBooks(list);
            return num>0 ? Result.ofSuccess("批量修改库存和价格成功", num) :
                Result.ofFailed("批量修改库存和价格失败");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return Result.ofFailed("批量修改库存和价格失败");
        }
    }
}
